package com.skit.zixing.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;

import com.journeyapps.barcodescanner.ViewfinderView;
import com.journeyapps.barcodescanner.camera.CameraManager;
import com.skit.zixing.R;

public class CustomViewfinderView extends ViewfinderView {
    private Paint boxPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private int i = 0;
    private Rect rect;
    private Bitmap scan_light_bitmap;

    public CustomViewfinderView(Context context, AttributeSet attrs) {
        super(context, attrs);
//        paint.setColor(Color.GREEN);
//        paint.setStrokeWidth(10);
//        paint.setTextSize(50);
//        paint.setStyle(Paint.Style.STROKE);
        scan_light_bitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.scan_light);
        boxPaint.setStyle(Paint.Style.FILL);
        boxPaint.setStrokeWidth(10);
        TypedValue typedValue = new TypedValue();
        context.getTheme().resolveAttribute(R.attr.colorAccent, typedValue, true);
        boxPaint.setColor(typedValue.data);
//        paint.setAlpha(255 >> 1);
//        boxPaint.setAlpha(255 >> 1);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    public void onDraw(Canvas canvas) {
        refreshSizes();
        if (framingRect == null || previewFramingRect == null) {
            return;
        }
//        refreshSizes()
        int measuredWidth = getMeasuredWidth();//Math.min(getMeasuredWidth(), getMeasuredHeight());
        int measuredHeight = getMeasuredHeight();//Math.max(getMeasuredWidth(), getMeasuredHeight());

        if (rect == null) {
//            int left = (measuredWidth / 2) / 2;
//            int right = measuredWidth - left;
//            int top = (measuredHeight / 2) / 2;
//            int bottom = measuredHeight - top;
//            int width = right - left;
//            int height = width;
//            int i = (int) ((measuredHeight / 2.5) - (height / 2));
            int left = framingRect.left;
            int right = framingRect.right;
            int top = framingRect.top;
            int bottom = framingRect.bottom;
            rect = new Rect(left, top, right, bottom);
        }

        Rect scanRect = new Rect(rect.left, rect.top + i, rect.right,
                rect.top + i + 30);
        canvas.drawBitmap(scan_light_bitmap, null, scanRect, paint);

        if (scanRect.bottom > rect.bottom - 30) {
            i = 0;
        }

//        canvas.drawLine(rect.left, i, rect.right, i, paint);
        i += 5;
//        canvas.drawRect(rect, boxPaint);
        drawFrameBounds(canvas, rect);
        invalidate();
//        super.onDraw(canvas);
    }

    private void drawFrameBounds(Canvas canvas, Rect frame) {

        /*paint.setColor(Color.WHITE);
        paint.setStrokeWidth(2);
        paint.setStyle(Paint.Style.STROKE);

        canvas.drawRect(frame, paint);*/
//        boxPaint.setColor(this.boxPaint.getColor());
//        boxPaint.setStyle(Paint.Style.FILL);

        int corWidth = 15;
        int corLength = 65;

        // 左上角
        canvas.drawRect(frame.left, frame.top, frame.left + corWidth, frame.top
                + corLength, boxPaint);
        canvas.drawRect(frame.left, frame.top, frame.left
                + corLength, frame.top + corWidth, boxPaint);
        // 右上角
        canvas.drawRect(frame.right - corWidth, frame.top, frame.right,
                frame.top + corLength, boxPaint);
        canvas.drawRect(frame.right - corLength, frame.top,
                frame.right, frame.top + corWidth, boxPaint);
        // 左下角
        canvas.drawRect(frame.left, frame.bottom - corLength,
                frame.left + corWidth, frame.bottom, boxPaint);
        canvas.drawRect(frame.left, frame.bottom - corWidth, frame.left
                + corLength, frame.bottom, boxPaint);
        // 右下角
        canvas.drawRect(frame.right - corWidth, frame.bottom - corLength,
                frame.right, frame.bottom, boxPaint);
        canvas.drawRect(frame.right - corLength, frame.bottom - corWidth,
                frame.right, frame.bottom, boxPaint);
    }
}
